OCR Xpress for Linux - Updated
Advanced Usage
User Guide > How To > Advanced Usage

OCR Xpress for Linux provides support for post-OCR operations. Via the API, applications have indirect access to the OCR Xpress internal structured OCR results. The structured data is a post-OCR model of the text content of the image.

After an image has been loaded and processed, OCR Xpress for Linux maintains a structured data model of the recognized text. The structured text data is referred to as the “document”. The following examples show a number of ways the data in the "document" may be accessed and how it can be used.

To demonstrate, the following code snippets will OCR an image and record the number of times the word “Hello” appears in the image and the location of each occurrence. 

  1. Load and process the image:
    Copy Code
    intptr_t dib = 0;
    OCRX_Result document = 0;
    OCRX_RecognitionParameters params = OCRX_DefaultRecognitionParameters;
    OCRX_load_file("FilePathOfImageToOCR.bmp", &dib);
    OCRX_recognize_to_memory(params, dib, &document);
    OCRX_free_dib(dib);
    
  2. Get the word count for the first page of the document:
    Copy Code
    …
    OCRX_Result page = 0;
    int32_t pageWordCount = 0;
    // Get a handle to the first page.
    OCRX_get_descendant_result(document, OCRX_ResultType_Page, 0, &page);
    // Get the word count for the first page.
    OCRX_get_descendant_result_count(page, OCRX_ResultType_Word, &pageWordCount);
    
  3. Find the words which match the test word:
    Copy Code
    const char* testWord = “Hello”; // Test word we are looking for.
    // Search each word of the first page for a test word match.
    for(int32_t wordIdx = 0; wordIdx < pageWordCount; wordIdx += 1)
    {
        // Get a handle to the word.
        OCRX_Result word = 0;
        OCRX_get_descendant_result(page, OCRX_ResultType_Word, wordIdx, &word);
          
        // Get the number of bytes required to hold the word.
        int32_t bufferSize = 0;
        OCRX_get_utf8_text_size(word, &bufferSize);
          
        // Get the text of the word.
        char* buffer = (char*)malloc(bufferSize);
        OCRX_get_utf8_text(word, buffer, bufferSize);
          
        // Test for a match.
        if(strcmp(testWord, buffer) == 0)
        {
            OCRX_Rectangle area = {0, 0, 0, 0};
            OCRX_get_area(word, &area);
            // Do something with the match.
        }
        free(buffer);
    }
    // Free the memory associated with the document.
    OCRX_free_document_result(&document);